package lnx

/*
#cgo CPPFLAGS: -fPIC -I../include
#cgo linux LDFLAGS: -fPIC -L${SRCDIR} -Wl,-rpath ${SRCDIR} -lhs_quote -lstdc++

#include "HSDataType.h"
#include "HSStruct.h"
void* qCreateApi();
void* qCreateSpi();
void* qGetVersion();
[[ range .Fn ]]// [[ .Comment ]]
void* q[[ .FuncName ]]([[ range $i, $v := .FuncFields ]][[ if gt $i 0 ]], [[ end ]][[ .FieldType|struct_Type ]] [[ .FieldName ]][[ if or (eq .FieldName "pReqDepthMarketDataSubscribe") (eq .FieldName "pReqDepthMarketDataCancel") ]][][[ end ]][[end]]);
[[ end ]]
[[ range .On ]]// [[ .Comment ]]
void qSetOn[[ .FuncName ]](void*, void*);
void q[[ .FuncName ]](void* spi[[ range $i, $v := .FuncFields ]], [[ .FieldType|struct_Type ]] [[ .FieldName ]][[ end ]]);
[[ end ]]
#include <stdlib.h>
#include <stdint.h>
*/
import "C"

import (
	"unsafe"

	"gitee.com/haifengat/gohs/def"
)

var q *Quote

// Quote 行情接口
type Quote struct {
	api unsafe.Pointer

    [[ range .On]]// [[ .Comment ]]
    On[[ .FuncName ]] func([[ range $i,$v := .FuncFields ]][[ if gt $i 0 ]], [[ end ]][[ .FieldName|trimStar ]] [[ .FieldType|ctp_type ]][[ end ]])
    [[ end]]
}

/*注册前置地址
  连接UFT系统时地址前缀为tcp://
  连接UST系统时地址前缀为ust://
  行情API 初始化
  连接UFT系统时传入许可证路径
  连接UST系统时无需许可证
*/
func (q *Quote) ReqConnect(addr, licFile string) {
	addrCopy := make([]byte, len(addr))
	copy(addrCopy, addr) // 传递副本
	front := C.CString(string(addrCopy))
	C.qRegisterFront(q.api, front)
	defer C.free(unsafe.Pointer(front))
	/// Description: 初始化连接
	///              pszLicFile            通讯证书
	///              pszSafeLevel          安全级别
	///              pszPwd                通讯密码
	///              pszSslFile            SSL证书
	///              pszSslPwd             SSL密码
	// void* qInit(void* api, const char *pszLicFile, const char *pszSafeLevel, const char *pszPwd, const char *pszSslFile, const char *pszSslPwd);
	licCopy := make([]byte, len(licFile))
	copy(licCopy, []byte(licFile))
	lic := C.CString(string(licCopy))
	space := ""
	cSpace := C.CString(space)
	defer C.free(unsafe.Pointer(cSpace))
	C.qInit(q.api, lic, cSpace, cSpace, cSpace, cSpace)
		// C.qJoin(q.api)
}

func (q *Quote) ReleaseAPI() {
	C.qRegisterSpi(q.api, nil)
	C.qReleaseApi(q.api)
	q.api = nil
}

func (q *Quote) ReqSubMarketData(instrument string) {
	f := def.CHSReqDepthMarketDataField{}
	copy(f.InstrumentID[:], []byte(instrument))
	C.qReqDepthMarketDataSubscribe(q.api, (*C.struct_CHSReqDepthMarketDataField)(unsafe.Pointer(&f)), C.int(1), C.int(1))
}

// NewQuote 实例化
func NewQuote() *Quote {
	q = new(Quote)

	q.api = C.qCreateApi()
	spi := C.qCreateSpi()
	C.qRegisterSpi(q.api, spi)

    [[ range .On ]]C.qSetOn[[ .FuncName ]](spi, C.q[[ .FuncName ]])
    [[ end ]]
	return q
}
[[ range .On ]]
//export q[[ .FuncName ]]
func q[[ .FuncName ]](spi unsafe.Pointer[[ range $i, $v := .FuncFields ]], [[ .FieldName|trimStar ]] [[ .FieldType|C_struct]][[ end ]]) {
    if q.On[[ .FuncName ]] != nil{
        q.On[[ .FuncName ]]([[ range $i,$v := .FuncFields ]][[ if gt $i 0 ]], [[ end ]][[ ctp_param .FieldType .FieldName ]][[ end ]])
    }
}
[[ end ]]
